package itsbank;


import accounts.Account;
import accounts.CheckingAccount;
import banks.AType;
import banks.CapitalMarketControl;
import database.DatabaseManager;
import exceptions.NotEnoughMoneyException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 * customers can open accounts at a bank, and perform actions like transactions etc.
 * @author Mike
 */

public class Customer {
    /**
     * the ID of the customer
     */
    public int ID;

    /**
     * the name of the customer
     */
    public String name;
    /**
     * the adress of the customer
     */
    public String street;
    /**
     * the phone number of the customer, may contain special characters for formating
     */
    public String phone;
    private int pin;
    private ArrayList<Account> listOfAccounts;
    private ArrayList<Customer> businessPartners;
    private int moneyInCash;
    private int customerId;
    private Random rand;

    /**
     * create a new customer
     * @param _ID the id of the customer
     * @param _name the name of the customer
     * @param _pin the pin of the customer
     * @param _phone the phone number of the customer
     * @param _adresse the adresse of the customer
     * @param _money the money in cash
     */
    public Customer (int _ID, String _name, String _adresse,String _phone,int _pin,int _money) {
        ID = _ID;
        name = _name;
        street = _adresse;
        phone = _phone;
        pin = _pin;
        moneyInCash = _money;
        rand = new Random();
        moneyInCash = rand.nextInt(1000);
        listOfAccounts = new ArrayList<Account>();
        businessPartners = new ArrayList<Customer>();
    }
    /**
     * init of the account and business partner information
     * not done in constructor because recursion lets to
     * stack overflow, laziely init when needed
     * @throws SQLException
     */
    private void InitData() throws SQLException {
        ResultSet rs = DatabaseManager.getInstance().query("SELECT * FROM account WHERE OwnerID = "+ID);
        while (rs.next()) {
            listOfAccounts.add(DatabaseManager.getInstance().getAccountByNumber(rs.getInt("AccountNumber")));
        }
        rs = DatabaseManager.getInstance().query("SELECT * FROM isPartnerOf WHERE CustomerID ="+ID);
        while (rs.next()) {
            businessPartners.add(DatabaseManager.getInstance().getCustomerByID(rs.getInt("PartnerID")));
        }
    }
    /**
     * returns a list of accounts posessed by the customer
     * @return the list of accounts
     */
    public ArrayList<Account> getAccounts () {
        return listOfAccounts;
    }
    /**
     * returns a list of business partners
     * @return the list of business partners
     */
    public ArrayList<Customer> getBusinessPartners () {
        return businessPartners;
    }
    /**
     * performe some random operations
     * @throws NotEnoughMoneyException can occur if there is not enough money on an account
     */
    public void doSomething() throws Exception, NotEnoughMoneyException {
        if (businessPartners.size() == 0) {
            InitData();
        }
        else if (listOfAccounts.isEmpty()) {
            String bankName = CapitalMarketControl.getInstance().getRandomBank().openAccount(AType.Checking, this);
             System.out.println(name+": I just opened a CheckingAccount in " + bankName);
        }
        else {
            CheckingAccount acc = ((CheckingAccount)listOfAccounts.get(0));
            int nextStep = rand.nextInt(4);
            if (nextStep == 0) {
                if (acc.getBalance() > 0) {
                    int rnd = rand.nextInt(acc.getBalance())+1;
                    acc.withdraw(rnd);
                    System.out.println(name+": I just withdraw "+rnd);
                }
                else {
                    System.out.println(name+": Oh dear, I have no more money left on my account!");
                }
            }
            else if (nextStep == 1) {
                int rnd = rand.nextInt(moneyInCash);
                if (rnd > 0) {
                    acc.deposit(rnd);
                    System.out.println(name+": I just deposited "+rnd);
                }
                else {
                    System.out.println(name+": Oh dear, I have no money left to deposit!");
                }
            }
            else if (nextStep == 2) {
                Customer partner = businessPartners.get(0);
                if (acc.getBalance() > 0) {
                    int rnd = rand.nextInt(acc.getBalance())+1;
                    if (partner.getAccounts().isEmpty()) {
                       System.out.println(name+":My Partner "+partner.name+" has not yet opened an account...");
                    }
                    else {
                        acc.transferTo(partner.getAccounts().get(0), rnd);
                        System.out.println(name+": I just transfered "+rnd+" to pay a bill from "+partner.name+"!");
                    }
                }
                else {
                    System.out.println(name+": Oh dear, I have no money to pay my bills from "+partner.name+"!");
                }
            }
            else {
                System.out.println(name+": I just checked my balance! Its "+acc.getBalance());
            }
        }
    }
}

